package sz.szsmk.citizencard.mvp.presenter;

import android.util.Log;

import com.google.gson.Gson;

import javax.inject.Inject;

import rx.functions.Action1;
import sz.szsmk.citizencard.DaggerApplication;
import sz.szsmk.citizencard.log.L;
import sz.szsmk.citizencard.model.network.GsdxNetApi;
import sz.szsmk.citizencard.model.network.bean.request.IBaseRequest;
import sz.szsmk.citizencard.model.network.bean.request.UserBehaviorBrowseReq;
import sz.szsmk.citizencard.model.network.bean.request.UserBehaviorReq;
import sz.szsmk.citizencard.model.network.bean.response.BaseBean;
import sz.szsmk.citizencard.model.network.bean.response.QueryMainAdRes;
import sz.szsmk.citizencard.model.network.bean.response.TagConfigRes;
import sz.szsmk.citizencard.mvp.contract.ILodingContact;
import sz.szsmk.citizencard.retrofitrxjava.ErrorAction;
import sz.szsmk.citizencard.retrofitrxjava.NetworkDateSources;
import sz.szsmk.citizencard.retrofitrxjava.SubscribeResult;
import sz.szsmk.citizencard.util.ACache;
import sz.szsmk.citizencard.util.AndroidUtils;
import sz.szsmk.citizencard.util.Constant;
import sz.szsmk.citizencard.util.DeviceUtil;
import sz.szsmk.citizencard.util.IObjectSignatureUtil;

/**
 * Copyright (c)2018
 * author: zdh
 * created on: 2018/8/27 14:20
 * description:
 */
public class LoadingPresenter extends IBasePresenter
        implements ILodingContact.ILodingPresenter<ILodingContact.ILodingView> {

    private NetworkDateSources mNetworkDateSource;
    GsdxNetApi gsdxNetApi;

    @Inject
    public LoadingPresenter(GsdxNetApi gsdxNetApi) {
        this.gsdxNetApi = gsdxNetApi;

        this.mNetworkDateSource = NetworkDateSources.getInstance();
    }

    @Override
    public void queryMainAd() {

        IBaseRequest baseRequest = IObjectSignatureUtil.getBaseRequest();
        String json = new Gson().toJson(baseRequest);
        L.e(Constant.TAG, "queryMainAd:" + json);
        mNetworkDateSource.queryMainAd(baseRequest, new SubscribeResult<QueryMainAdRes>() {
            @Override
            protected void onOk(QueryMainAdRes response) {
                if (response != null) {
                    L.e(Constant.TAG, (mView==null)+"");
                    ((ILodingContact.ILodingView) mView).queryMainAdSucc(response);
                }
            }

            @Override
            protected void onFailure(QueryMainAdRes response) {
                super.onFailure(response);
                L.e(Constant.TAG,response.getMessage());
            }
        }, new ErrorAction() {
            @Override
            public void onCall(Throwable throwable) {
                L.e(Constant.TAG,throwable.getMessage());

            }
        });
    }

    @Override
    public void userBehaviorBrowse(int id) {
        UserBehaviorBrowseReq userBehaviorBrowseReq = new UserBehaviorBrowseReq();
        userBehaviorBrowseReq.setModularId(id);
        userBehaviorBrowseReq.setAccessToken(ACache.get(DaggerApplication.getIns()).getAsString("access_token"));

        mNetworkDateSource.userBehaviorBrowse(userBehaviorBrowseReq, new Action1<BaseBean>() {
            @Override
            public void call(BaseBean baseBean) {
            }
        }, new ErrorAction() {
            @Override
            public void onCall(Throwable throwable) {
            }
        });
    }
    @Override
    public void userBehaviorActivity(){
        UserBehaviorReq baseRequest = new UserBehaviorReq();
        if(ACache.get(DaggerApplication.getIns()).getAsString("access_token")!=null){
            baseRequest.setAccessToken(ACache.get(DaggerApplication.getIns()).getAsString("access_token"));
        }else{
            baseRequest.setAccessToken("");
        }

        baseRequest.setDeviceId(AndroidUtils.getAndroidId(DaggerApplication.getIns()));
        mNetworkDateSource.userBehaviorActive(baseRequest, new SubscribeResult<BaseBean>() {
            @Override
            protected void onOk(BaseBean response) {
                Log.i("tag","ok");
            }

            @Override
            protected void onFailure(BaseBean response) {
                super.onFailure(response);
            }
        }, new ErrorAction() {
            @Override
            public void onCall(Throwable throwable) {

            }
        });
    }



}
